﻿Imports System.Threading

Public Class MyService
    Private _ti As Timer
    Private _Interval As Integer = 15000
    Private _mytaks As MyTasks

    ''' <summary>
    ''' 启动服务
    ''' </summary>
    ''' <param name="args"></param>
    ''' <remarks></remarks>
    Protected Overrides Sub OnStart(ByVal args() As String)

        ' 请在此处添加代码以启动您的服务。此方法应完成设置工作，
        ' 以使您的服务开始工作。
        _mytaks = New MyTasks("default")

        _Interval = Configuration.ConfigurationManager.AppSettings("timer")
        If String.IsNullOrEmpty(_Interval) Then
            _Interval = 10000
        End If

        If _ti Is Nothing Then
            Dim tiCallback As TimerCallback = AddressOf Start
            _ti = New Timer(tiCallback, args, 0, _Interval)
        Else
            _ti.Change(0, _Interval)
        End If


    End Sub

    Sub Start()

        ' to do something ...
        Try
            _mytaks.StartTask()
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try

    End Sub

    Protected Overrides Sub OnStop()
        ' 在此处添加代码以执行任何必要的拆解操作，从而停止您的服务。
        ' 停止定时器
        If Not _ti Is Nothing Then
            _ti.Change(0, System.Threading.Timeout.Infinite)
            _ti = Nothing
        End If

    End Sub

End Class
